var $;
var form;
var layer;
var upload;
$(function() {
    layui.use(['form', 'layer','upload','element'], function () {
        //获取jquery的$
        $=layui.jquery;
        //获取form表单对象，封装方法获取form表单的
        form=layui.form;
        //获取layer对象
        layer=layui.layer;
        upload=layui.upload;

        //获取登录用户的信息
        $.ajax({
            url:"/login/selUserContent",
            type:"post",
            dataType:"json",
            data:{},
            success:function(data){
                $("#userName").val(data.obj.account);
                $("#email").val(data.obj.email);
                $("#userId").val(data.obj.userId);
                $("#userPwd").val(data.obj.password);
                if(data.obj.avatarUrl){
                    $('#layui_i1').css("display","none");
                    $("#imgId").attr('src',data.obj.avatarUrl);
                }
            }
        });

        //编辑按钮
        $("#butId").on("click",function(){
            $("#pudId").css("display","block");
            $("#butId").css("display","none");
            $("#userName").removeAttr("readonly");
            $("#email").removeAttr("readonly");
            $("#ppwd").removeAttr("readonly");
            $("#npwd").removeAttr("readonly");
            $("#npwd2").removeAttr("readonly");
        });

        //点击上传图片
        $("#updimga").on("click",function(){
            $("#imgbut").click();
        });

        //图片上传
            var uploadInst = upload.render({
                elem: '#imgbut'
                ,accept:'images'
                ,size: 5000 //限制文件大小，单位 KB
                , url: '/login/updimg' //上传接口
                , before: function (obj) {//预读本地文件示例
                    var id=$("#userId").val();
                    this.data={'id':id}//参数

                    obj.preview(function (index, file, result) {
                        $('#layui_i1').css("display","none");//隐藏默认图片
                        $('#imgId').attr('src', result); //图片链接（base64）
                        $("#imgurlId").val(result);
                    });
                }
                , done: function (res) {//上传成功的一些操作
                    if(res.code>-1){
                        var url=$("#imgurlId").val();

                        layer.msg('上传完毕', {icon: 1});
                    }else{
                        layer.msg('上传失败,请重试', {icon: 2});
                    }
                }
                , error: function () {//失败
                    layer.msg('上传失败', {icon: 2});
                }
            });


        //提交按钮
        $("#pudId").on("click",function(){
            //判断旧密码是否正确&新密码是否一致
            var ppwd=$('#ppwd').val();
            var userPwd=$('#userPwd').val();
            var pwd1=$('#npwd').val();
            var pwd2=$('#npwd2').val();
            $.post("/login/selMD5pwd", {"pwd": ppwd}, function (res) {
                if(res.obj==userPwd && res.obj!==""){
                    if(ppwd!=pwd1){
                        if(pwd1==pwd2 && pwd1!=="" && pwd2!==""){
                            var useremail=$("#email").val();
                            //发送邮件
                            sendmail(useremail);

                            //验证弹窗
                            layer.open({
                                type: 1,
                                title: "验证码验证",
                                area: ['420px', '250px'],
                                shade: 0.4,
                                offset: 't',
                                content: $('#layerId'),
                                success: function (layero, index) {
                                    //选择元素,并设置值
                                    $("#userEmailId").val(useremail);
                                },
                                btn: ['确定', '取消']
                                ,yes: function(index, layero){
                                    var eamil1=$("#emailId").val();
                                    $("#mailId2").val(eamil1);

                                    //判断验证码是否正确
                                    var mail=$("#mailId").val();
                                    var mail2=$("#mailId2").val();
                                    if(mail==mail2){
                                        layer.msg('验证成功');
                                        //提交后台
                                        updform();
                                    }else{
                                        layer.alert('验证码错误！', {icon: 2});
                                    }
                                    //关闭弹窗
                                    layer.close(index);
                                }
                                ,btn2: function(index, layero){
                                    //关闭弹窗
                                    layer.close(index);
                                }
                            });
                        }else{
                            layer.alert('新密码不一致！', {icon: 2});
                        }
                    }else{
                        layer.alert('新密码不能同旧密码一致', {icon: 2});
                    }
                }else{
                    layer.alert('旧密码错误！', {icon: 2});
                }
            });
        });
        
        
        //发送邮件验证码
        function sendmail(useremail) {
            $.ajax({
                url:"/login/selmail",
                type:"post",
                dataType:"json",
                data:{"toemail":useremail},
                success:function(data){
                    $("#mailId").val(data.obj);
                }
            });
        }


        //提交到后台
        function updform(){
            var formcont=new FormData(document.getElementById("accountForm"));
            $.ajax({
                url:"/login/updUser",
                type:"post",
                dataType:"json",
                data:formcont,
                contentType:false,//不转换为其他格式，是JS的form表单对象
                processData:false,
                success:function(data){
                    //console.log(data);
                    if(data.code>-1){
                        layer.alert('提交成功！', {icon: 1}, function(index){
                            //关闭窗口并刷新页面
                            layer.close(index);
                            location.reload(true);
                        });
                    }else{
                        layer.alert('提交失败！', {icon: 2});
                    }
                    //添加完成后赋值id,name。判断是否走下一步
                    //$("#newsbId").val(data.eqid);
                    //$("#newsb_name").val(data.eqname);
                }
            });
        }
    });

});